package com.example.demo.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import com.example.demo.util.StringType;

@Controller
public class UserController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	UserService userService;

	/**
	 * 跳转注册页面
	 */
	@RequestMapping("/register")
	public String toRegister() {
		return "register";
	}

	// 注册用户
	@PostMapping("/user/reg")
	@ResponseBody
	public ArrayList<String> doRegister(@ModelAttribute("user") User user, @RequestParam String pwd2) {
		logger.info("----用户控制器：doRegister");
		logger.info(user.toString());

		// 校验参数是否合法，用户名无要求；密码长度，密码是否相同
		// 校验成功，调用Service；失败直接返回信息
		ArrayList<String> reginfo = new ArrayList<String>();
		if ((user.getPwd()).equals(pwd2)) {

			if ((userService.userReg(user)) == 1) {
				reginfo.add("注册成功");
				logger.info("注册成功");
				return reginfo;
			} else {
				reginfo.add("用户名已被占用或其他错误，注册失败");
				return reginfo;
			}
		} else {
			reginfo.add("两次密码输入不一致");
			return reginfo;
		}
//		User u =new User();
//		u.setUid(1000000000);
//		u.setEmail("12@qq.com");
//		u.setPwd("123456");
//		u.setUsername("test");
//		UserService.addStudent(u);
	}

	// 跳转登录页
	@RequestMapping("/login")
	public String toLogin() {
		return "login";
	}

	@PostMapping("/user/login")
	public String doLogin(@RequestParam String namemail, @RequestParam String pwd, HttpSession session) {
		logger.info("----用户控制器：doLogin:");
		User dbUser = new User();
		// 判断namemail类型
		// 根据传入的user中的用户名或邮箱调用进行查找
		dbUser.setPwd(pwd);
		if(StringType.isEmail(namemail)) {
			dbUser.setEmail(namemail);
		}
		else {
			dbUser.setUsername(namemail);
		}
		dbUser = userService.userLogin(dbUser);
//		System.out.println((dbUser==null));
//		new出来的对象不为空
		// 判断dbUser是否为空，空则跳转登录页，非空跳转首页，并保存在session中
		
		if (dbUser == null) {
			return "login";
		} else {
			session.setAttribute("user", dbUser);
			User sUser = new User();
			sUser = (User)session.getAttribute("user");
			logger.info("----"+sUser.toString());
			return "redirect:/";
		}
	}
	
	/**
	 * 跳转用户信息
	 */
	@RequestMapping("/account")
//	@ResponseBody
	public String toAccount(Model model, HttpSession session) {
		if (session.getAttribute("user") == null) {
			return "login";
		}
		User u = (User) session.getAttribute("user");
		List<User> Friends=userService.getFriendsByUser(u);
		List<User> Follows=userService.getFollowByUser(u);
		List<User> BeFollows=userService.getBeFollowedByUser(u);
		session.setAttribute("friends", Friends);
		session.setAttribute("follows", Follows);
		session.setAttribute("befollows", BeFollows);
		return "account";
	}
	
//	@PostMappering("/account/friend")
//	public String
}